﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Net;

namespace BookFinder
{



    public class HttpGetter
    {
        private int count = 0;
        private StringBuilder sb = new StringBuilder();
        private string tempString = null;
        // used on each read operation
        private byte[] buf = new byte[8192];

        public String HttpGetRequest(String requestString)
        {
            Stream resStream = null;
            // prepare the web page we will be asking for
            try
            {

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestString);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                resStream = response.GetResponseStream();
            }
            catch (UriFormatException e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }

            // execute the request



            if (resStream == null)
            {
                return "";
            }

            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.ASCII.GetString(buf, 0, count);

                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0); // any more data to read?

            return sb.ToString();
        }
    }
}